package com.cljz.peipei.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.cljz.peipei.R;
import com.cljz.peipei.base.App;
import com.cljz.peipei.base.BaseActivity;
import com.cljz.peipei.model.net.bean.ResponseBean;
import com.cljz.peipei.present.activity.LoginP;
import com.cljz.peipei.utils.CommonUtils;
import com.cljz.peipei.utils.T;
import com.jakewharton.rxbinding.view.RxView;
import com.jakewharton.rxbinding.widget.RxTextView;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.Map;

import butterknife.BindView;
import cn.droidlover.xdroidmvp.router.Router;
import rx.functions.Action1;

/**
 * Created by apple on 2017/4/17.
 */

public class LoginActivity extends BaseActivity<LoginP> {
    @BindView(R.id.iv_qq)
    ImageView ivQq;
    @BindView(R.id.btn_get_sms)
    Button btnGetSms;
    @BindView(R.id.v_line)
    View vLine;
    @BindView(R.id.et_number)
    EditText etNumber;
    @BindView(R.id.ll_number)
    LinearLayout llNumber;
    private String phone;
    //友盟实现第三方登录
    private UMShareAPI umShareAPI;


    @Override
    public void initData(Bundle savedInstanceState) {
        umShareAPI = UMShareAPI.get(this);

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public LoginP newP() {
        return new LoginP();
    }

    @Override
    protected void initListener() {
        super.initListener();
        RxTextView.textChanges(etNumber)
                .compose(this.<CharSequence>bindToLifecycle())
                .subscribe(new Action1<CharSequence>() {
                    @Override
                    public void call(CharSequence charSequence) {
                        if (charSequence.length() == 11) {
                            if (!CommonUtils.checkPhone(charSequence.toString())) {
                                T.getInstance().showShort("请输入正确的手机号");
                            }
                        }
                    }
                });
        RxView.clicks(btnGetSms)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        showLoadingDialog();
//                Router.newIntent((Activity) App.getInstance()).putString("phone", phone).to(EditSmsCodeActivity.class).launch();

                        if (CommonUtils.isNetworkAvailable()) {
                            phone = etNumber.getText().toString().trim();
                            if (CommonUtils.checkPhone(phone)) {
                                getP().sendSmsCode(phone);
                            } else {
                                T.getInstance().showShort("请输入正确的手机号");
                            }
                        } else {
                            T.getInstance().showShort("请开启网络");
                        }
                    }
                });
        RxView.clicks(ivQq)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        showLoadingDialog();
                        umShareAPI.getPlatformInfo((Activity) App.getInstance(), SHARE_MEDIA.QQ, umAuthListener);
                    }
                });
    }

    public void sendSmsSucceed(ResponseBean peopleNumBean) {
        dismissLoadingDialog();
        Router.newIntent((Activity) App.getInstance()).putString("phone", phone).to(EditSmsCodeActivity.class).launch();
//        Router.pop(this);
    }

    public void sendSmsFail(String error) {
        dismissLoadingDialog();
        T.getInstance().showShort(error);
    }

    private UMAuthListener umAuthListener = new UMAuthListener() {
        @Override
        public void onStart(SHARE_MEDIA platform) {
            //授权开始的回调

        }

        @Override
        public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
            if (platform == SHARE_MEDIA.QQ) {
                T.getInstance().showShort("QQ登录成功");

            } else if (platform == SHARE_MEDIA.WEIXIN) {
                T.getInstance().showShort("微信登录成功");
            }
            Router.newIntent((Activity) App.getInstance()).to(SetSexActivity.class).launch();
            getP().loginByThird(platform, data);

        }

        @Override
        public void onError(SHARE_MEDIA platform, int action, Throwable t) {
            if (platform == SHARE_MEDIA.QQ) {
                T.getInstance().showShort("QQ登录失败");

            } else if (platform == SHARE_MEDIA.WEIXIN) {
                T.getInstance().showShort("微信登录失败");
            }
        }

        @Override
        public void onCancel(SHARE_MEDIA platform, int action) {
            T.getInstance().showShort("取消登陆");

        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }
}
